public class Rotate {
    // leetcode 轮转数组
    // https://leetcode.cn/problems/rotate-array/description/?envType=study-plan-v2&envId=top-100-liked
    // 思路：将数组中的元素轮转k次的结果可以类似于将[0,len-k-1]之间的元素逆序
    // 然后将[len-k,len-1]之间的数组进行逆序，最后将整个数组进行逆序
    private void reverse(int[] nums, int left, int right) {
        while (left < right) {
            int tmp = nums[left];
            nums[left] = nums[right];
            nums[right] = tmp;
            left++;
            right--;
        }
    }

    public void rotate(int[] nums, int k) {
        int len = nums.length;
        reverse(nums,0,len-k%len-1);
        reverse(nums,len-k%len,len-1);
        reverse(nums,0,len-1);
    }
}
